// packageCommunity/componentsvideo-discuss/index.js
import{comments} from "../../../js/modules/mall.js"
import authorize from '../../../js/modules/authorize.js'
import {util,api} from '../../../js/util'; 
import { isEmpty } from "../../../js/newUtil.js";
const componentBehavior = require('../../../js/common/ComponentBehavior')
const app = getApp()

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    display:{
      type: Boolean,
      value: false
    },
    postid:{
      type:Number,
      value:false,
    },
    postName:{
      type:String,
      value:false
    },
    NoLoginShowInteraction: { // 无需登录可查看发现详情
      type: Boolean,
      value: true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgDomain: api.imgDomain,
    videoDomain:api.videoDomain,
    enableOss: api.enableOss,
    CommentList:[],//评论列表
    replayInfo:{
      InteractionId:'',//帖子ID
      CommentId:'',//评论ID
      ParentId:''//父级回复ID
    },
    replyHidden:false,//
    userReplyHidden:false,//回复
    replyIndex:0,//回复索引
    commentName:'',
    postType:'',//
    currentNum:0,//多少字
    replayCon:'',//回复内容
    allLookReply:[],//查看全部回复
    bottom:'',
    focus:false,//获取焦点
  },
  observers:{
    'postid':function(postid){
      // console.log("传过来的值",postid);
      if(postid!=0)this.getCommontList(postid)
    },
  },
  behaviors: [componentBehavior],
  /**
   * 组件的方法列表
   */
  methods: {
    btnClose:function(){
      this.setData({display:false})
    },
    
    /**
    * 评论列表
    */
    getCommontList:function(id){
      var that = this;
      var data = {
        InteractionId:id
      }
      comments.getPostCommentList(data,function(content){
        // console.log(content, '----评论列表');

        if (content.Success) {
          let storeInfo = authorize.getEntity() || {};
          let manager = storeInfo.manager || {}
          if (isEmpty(content) || isEmpty(content.Content)) {
            return
          }
          var data = content.Content;
          // console.log(storeInfo,'----info');
          for (var i = 0; i < data.data.length; i++) {
            // manager.ManagerId
            if ((data.data[i].FKFlag == storeInfo.fkFlag && data.data[i].ManagerId == manager.ManagerId) && data.data[i].IsAuthority) {
              data.data[i].userDel = true;
            } else {
              data.data[i].userDel = false;
            }
          }
          that.setData({
            CommentList: data
          })
        } else {
          // 朋友圈单页模式进入此页面，不提示跳转登录
          let appLaunchData = app.globalData.appLaunchData || {}
          if (appLaunchData.scene == 1154) {
            return
          }
          // 无需登录则不提示跳转
          if (that.data.NoLoginShowInteraction == true) {
            return
          }
          util.alert(content.Message);
        }
        // console.log(that.data.CommentList,'----');
      })
    },
    delReply(e){
      var that = this;
      var id = e.currentTarget.dataset.id
      var data = {Id:id};
      var list = that.data.allLookReply;
      comments.getReplyDel(data,function(content){
        if(content.Success){
          // console.log(content,'----del');
          for (var i = 0; i < list.length; i++) {
            if (list[i].Id == id){
              list.splice(i, 1);
            }
          }
          that.setData({
            allLookReply:list
          })
        }else{
          util.alert(content.Message);
        }
      })
    },
    /**
    * 我来评论
    */
    bindReply:function(e){
      let {replayInfo} = this.data;
      replayInfo.InteractionId = e.currentTarget.dataset.postid;
      replayInfo.CommentId = e.currentTarget.dataset.commentid; 
      replayInfo.ParentId = e.currentTarget.dataset.parentid; 
      var name = e.currentTarget.dataset.name;
      var type = e.currentTarget.dataset.type;
      this.setData({
        userReplyHidden:true,
        replayInfo:replayInfo,
        commentName:name,
        postType:type,
        focus:true
      })

    },
    /**
  * 获取焦点--失去焦点  
  */
  focus:function(e){
    this.setData({
      bottom:e.detail.height*2-36
    })
  },
  blur:function(e){
    this.setData({
      bottom:0
    })
  },
    /**
    * 输入回复
    */
    iptCon:function(e){
      let value = e.detail.value;
      this.setData({
        replayCon:value,
        currentNum:value.length
      })
    },
    /**
    * 关闭回复
    */
    closeModel:function(){
      this.setData({
        userReplyHidden:false,
        replayCon:''
      })
    },
    /**
    * 发布
    */
    btnRelease:function(e){
      var that = this;
      var replayInfo = that.data.replayInfo;
      if(!that.data.replayCon){
        util.tips('请输入内容');
        return;
      }
      // console.log(replayInfo,'----info');
      if(that.data.postType == 0){//评论帖子
        var data = {
          InteractionId:replayInfo.InteractionId,//帖子ID
          Content:that.data.replayCon//评论内容
        };
        comments.getAddComment(data,function(content){
          // console.log(content,'----content');
          if(content.Success){
            that.setData({
              replayCon:'',
              currentNum:0,
              userReplyHidden:false
            })
            that.getCommontList(replayInfo.InteractionId);
          }else{
            util.tips(content.Message);
          }
        })
      }else{//回复
        var data = {
          InteractionId:replayInfo.InteractionId,//帖子ID
          CommentId:replayInfo.CommentId, //评论ID
          ParentId:replayInfo.ParentId, //父级回复ID
          Content:that.data.replayCon
        };
        var datas = {
          InteractionId:replayInfo.InteractionId,
          CommentId:replayInfo.CommentId
        };
        // console.log(data,'----',datas);
        comments.getAddReply(data,function(content){
          // console.log(content,'----content');
          if(content.Success){
            that.setData({
              replayCon:'',
              currentNum:0,
              userReplyHidden:false
            })
            that.replyList(datas);
            that.getCommontList(replayInfo.InteractionId); 
          }else{
            util.alert(content.Message);
          }
        })
      }
    },
    /**
    * 查看所有回复
    */
    bindAllReply:function(e){
      var that = this;
      var postid = e.currentTarget.dataset.postid;//帖子id
      var id = e.currentTarget.dataset.id;//评论Id
      var item = e.currentTarget.dataset.item;
      var index= e.currentTarget.dataset.index;
      // console.log(item,'---item');
      var data = {
        InteractionId:postid,
        CommentId:id
      };
      that.setData({
        itemReply:item,
        replyHidden:true,
        replyIndex:index
      })
      that.replyList(data);
    },
    replyList:function(data){
      var that = this;
      comments.getCommentList(data,function(content){
        if(content.Success){
          let list = content.Content.data;
          let storeInfo = authorize.getEntity() || {};
          let manager = storeInfo.manager || {}
          list.forEach(function(item){
            if((item.FKFlag == storeInfo.fkFlag && item.ManagerId == manager.ManagerId) && item.IsAuthority){
              item.userDel = true;
            }else{
              item.userDel = false;
            }
          })
          that.setData({
            allLookReply:list
          })
        }else{
          util.alert(content.Message);
        }
      })
    },
    /**
    * 查看回复
    */
    bindReplyClose:function(){
      this.setData({
        replyHidden:false
      })
    },
    /**
     *  删除指定评论记录
    */
    bindComment:function(e){
      var that = this;
      // var index = e.currentTarget.dataset.index;
      var id = e.currentTarget.dataset.id;
      var data = {CommentId:e.currentTarget.dataset.id};
      var list = this.data.CommentList;
      comments.getCommentRecord(data,function(content){
        if(content.Success){
          for (var i = 0; i < list.data.length; i++) {
            if (list.data[i].Id == id){
              list.data.splice(i, 1);
            }
          }
          that.setData({
            CommentList:list,
            replyHidden:false
          })
          // console.log(that.data.CommentList,'---删除');
        }else{
          util.alert(content.Message);
        }
      })
    },
    /**
     *  点赞
    */
    bindLike:function(e){
      var that = this;
      //0:帖子，1：评论
      var type = e.currentTarget.dataset.type;
      var id =  e.currentTarget.dataset.id;
      var status = e.currentTarget.dataset.status;// true - false
      var data = {Type:type,Id:id};
      var list = that.data.CommentList; 
      var replyType = e.currentTarget.dataset.replay;
      if(!status){
        //点赞
        comments.getUserFollow(data,function(content){
          if(content.Success){
            var index = e.currentTarget.dataset.index;
            list.data[index].Hots ++;
            list.data[index].IsSupport = true;
            that.setData({
              CommentList:list
            })
            if(replyType){
              var item = that.data.itemReply;
              item.Hots ++;
              item.IsSupport = true;
              that.setData({
                itemReply:item
              })
            }
          }else{
            util.alert(content.Message);
          }
        })
      }else{
        //取消点赞
        comments.getUserCancel(data,function(content) {
            var index = e.currentTarget.dataset.index;
            list.data[index].Hots --;
            list.data[index].IsSupport = false;
            that.setData({
              CommentList:list
            })
            if(replyType){
              var item = that.data.itemReply;
              item.Hots --;
              item.IsSupport = false;
              that.setData({
                itemReply:item
              })
            }
        })
      }
    },
  }
})